package com.ccsc.auth.service.impl;

import com.ccsc.auth.dao.ResourceDao;
import com.ccsc.auth.entity.Department;
import com.ccsc.auth.entity.Resource;
import com.ccsc.auth.entity.Role;
import com.ccsc.auth.service.ResourceService;
import com.ccsc.auth.utils.DataMap;
import com.ccsc.auth.utils.Page;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 资源service
 * Created by yyangs on 2017/2/15.
 */
@Service
public class ResourceServiceImpl implements ResourceService {
    @Autowired
    private ResourceDao resourceDao;

    @Override
    public List<Resource> getAllResources() throws Exception {
        return resourceDao.getAllResources();
    }

    @Override
    public List<Resource> getResourcesByLevel(Integer level) throws Exception {
        Resource resource=new Resource();
        resource.setLevel(level);
        return resourceDao.getResourceInfosByCondition(resource);
    }

    @Override
    public List<Resource> getResourcesByRoleId(Integer roleId) throws Exception {
        return resourceDao.getResourceInfosByRoleId(roleId);
    }

    @Override
    public List<Resource> getResourcesBySystemsId(Integer systemsId) throws Exception {
        return resourceDao.getResourceInfosBySystemsId(systemsId);
    }

    @Override
    public List<Resource> getResourcesByParentResourceId(Integer parentResourceId) throws Exception {
        Resource prarenResource=new Resource();
        prarenResource.setId(parentResourceId);
        Resource resource=new Resource();
        resource.setParentResource(prarenResource);
        return resourceDao.getResourceInfosByCondition(resource);
    }

    @Override
    public Page getResourcesByPage(DataMap params) throws Exception {
        List<Resource> resultList = resourceDao.getAllResources(new PageBounds(params.getInt("page"), params.getInt("pageSize")));
        PageList<Resource> pageList = (PageList<Resource>) resultList;
        return new Page(pageList);
    }

    @Override
    public Page getResourcesByPage(Resource resource, DataMap params) throws Exception {
        List<Resource> resultList = resourceDao.getResourceInfosByCondition(resource,new PageBounds(params.getInt("page"), params.getInt("pageSize")));
        PageList<Resource> pageList = (PageList<Resource>) resultList;
        return new Page(pageList);
    }

    @Override
    public Resource getResourceById(Integer id) throws Exception {
        Resource resource=new Resource();
        resource.setId(id);
        return resourceDao.getResourceInfoById(resource);
    }

    @Override
    public Integer createResource(Resource resource) throws Exception {
        return resourceDao.createResource(resource);
    }

    @Override
    public Integer updateResource(Resource resource) throws Exception {
        return resourceDao.updateResource(resource);
    }

    @Override
    public Integer deleteResourceById(Integer id) throws Exception {
        Resource resource=new Resource();
        resource.setId(id);
        return resourceDao.deleteResource(resource);
    }

    /**
     *通过一组role列表，有层级得返回资源树
     * @param roleList
     * @return
     */
    public List<Resource> getResourcelistByRolelist(List<Role> roleList) throws Exception{
        Set<Resource> resourceSet=new HashSet<Resource>();
        for (Role role:roleList) {
            List<Resource> resources=resourceDao.getResourceInfosByRoleId(role.getId());
            role.setResourceList(resources);
            for (Resource resource:resources) {
                resourceSet.add(resource);
            }
        }
        //转化资源树
        List<Resource> resourceList=new ArrayList<>();
        resourceList.addAll(resourceSet);
        resourceList=resourceSet2List(resourceList,null);
        return resourceList;
    }


    /**
     *将平行的资源树转化层级资源树
     * @param resourceList
     * @param parentNum
     * @return
     */
    public static List<Resource> resourceSet2List(List<Resource> resourceList,Integer parentNum){
        List<Resource> temp=new LinkedList<Resource>();
        if(resourceList.size()==0){return null;}
        for (int i=0; i<resourceList.size(); i++) {
            if(resourceList.size()==0){break;}
            Resource resource = resourceList.get(i);
            if(parentNum!=null) {
                if(resource.getParentResource()==null){continue;}
                if (parentNum==resource.getParentResource().getId()) {
                    resourceList.remove(i);
                    i--;
                    resource.setChildResource(resourceSet2List(resourceList, resource.getId()));
                    temp.add(resource);
                }
            }else {
                if (resource.getParentResource()==null) {
                    resourceList.remove(i);
                    i--;
                    resource.setChildResource(resourceSet2List(resourceList, resource.getId()));
                    temp.add(resource);
                }
            }
        }
        return temp;
    }

}
